@extends('layouts.app')

@section('title', $shop->shop_name.'-店铺详情')

@section('css-write')
<style type="text/css">
	.identitycardimg_sty{
		width:300px;
		display:block;
	}
	.qrcodeimg{
		cursor:pointer;
	}
</style>
@endsection

@section('content-fluid')

<div class="panel panel-profile">
	<div class="clearfix">
		<!-- LEFT COLUMN -->
		<div class="profile-left" style="width: 28%;">
			<!-- PROFILE HEADER -->
			<div class="profile-header">
				<div class="overlay"></div>
				<div class="profile-main" style="padding: 55px;@if(!empty($shop->shop_logo)) background-image:url({{$shop->shop_logo}}); background-position: center;@endif">
					
					<img src="{{$shop->shop_logo}}" class="img-circle" alt="{{$shop->shop_name}}" style="width: 100px;height: 100px;visibility: hidden;">
					
					<h3 class="name" style="visibility: hidden;">{{$shop->shop_name}}</h3>
					<!-- <span class="online-status status-available">炒饼</span> -->
				</div>
				<div class="profile-stat">
					<div class="row">
						<div class="col-md-3 stat-item">
							{{$shop->sell}} <span>今日销量</span>
						</div>
						<div class="col-md-3 stat-item">
							￥{{$shop->account}} <span>账户余额</span>
						</div>
						<div class="col-md-3 stat-item">
							{{$shop->ordertotal}} <span>总订单数</span>
						</div>
						<div class="col-md-3 stat-item">
							{{$extract_money}} <span>提现总额</span>
						</div>
					</div>
				</div>
			</div>
			<!-- END PROFILE HEADER -->
			<!-- PROFILE DETAIL -->
			<div class="profile-detail">
				<div class="profile-info">
					<h4 class="heading">店铺信息</h4>
					<ul class="list-unstyled list-justify">
						<li>入驻时间 <span>{{$shop->insert_time}}</span></li>
						<li>联系方式 <span>{{$shop->shop_tel}}</span></li>
						@if(session('region') < 3)
						<li>店主 <span><a href="/user-details?userid={{$shop->users_id}}">{{$shop->nicename??'未填写'}}</a></span></li>
						@endif
					</ul>
				</div>
				<div class="profile-info">
					<h4 class="heading">店铺地址</h4>
					<p>{{$shop->shop_address}}</p>
				</div>
				<div class="text-center">
					@if($shop->shop_run_status==1)
					<a href="javascript:void(0);" class="btn btn-primary suspend-business">暂停营业</a>
					@elseif($shop->shop_run_status==0)
					<a href="javascript:void(0);" class="btn btn-primary start-business">开始营业</a>
					@elseif($shop->shop_run_status==-1)
					<a href="javascript:void(0);" class="btn btn-primary start-business">开始营业</a>
					@endif
					@if(session('region') < 3)
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<a href="javascript:void(0);" class="btn btn-danger" onclick="editInfo()">修改信息</a>
					@endif
				</div>
			</div>
			<!-- END PROFILE DETAIL -->
		</div>
		<!-- END LEFT COLUMN -->
		<!-- RIGHT COLUMN -->
		<div class="profile-right">
			<h4 class="heading">{{$shop->shop_name}}</h4>
			<!-- AWARDS -->
			<div class="awards">
				<div class="row">
					<div class="col-md-3 col-sm-6">
						<div class="award-item">
							<div class="hexagon run-status">
								<!-- ban -->
								@if($shop->shop_run_status==1)
								<span class="fa fa-circle-o-notch fa-spin fa-3x fa-fw margin-bottom" title="营业中"></span>
								@elseif($shop->shop_run_status==0)
								<span class="fa fa-play award-icon" title="暂停营业"></span>
								@else
								<span class="fa fa-ban award-icon" title="暂停营业"></span>
								@endif
							</div>
							<span>营业状态</span>
						</div>
					</div>
					<div class="col-md-3 col-sm-6">
						<div class="award-item">
							<div class="hexagon">
								<span class="fa fa-thumbs-up award-icon"></span>
							</div>
							<span>店铺评分</span>
						</div>
					</div>
					<div class="col-md-3 col-sm-6">
						<div class="award-item">
							<div class="hexagon">
								<!-- address-book-o -->
								<!-- user-times -->
								<span class="fa fa-vcard-o award-icon" title="商户认证"></span>
							</div>
							<span>认证状态</span>
						</div>
					</div>
					<div class="col-md-3 col-sm-6">
						<div class="award-item">
							<div class="hexagon">
								<span class="lnr lnr-heart award-icon"></span>
							</div>
							<span>被收藏数</span>
						</div>
					</div>
				</div>
			</div>
			<!-- END AWARDS -->
			<!-- TABBED CONTENT -->
			<div class="custom-tabs-line tabs-line-bottom left-aligned">
				<ul class="nav" role="tablist">
					<li><a href="#tab-bottom-left1" role="tab" data-toggle="tab" tabindex="0">提现记录</a></li>
					<li><a href="#tab-bottom-left2" role="tab" data-toggle="tab" tabindex="1">订单明细</a></li>
					<li><a href="#tab-bottom-left3" class="tab-foodlist" role="tab" data-toggle="tab" tabindex="2">他家的饭</a></li>
					@if(session('region') < 3)
					<li><a href="javascript:;" onclick="urlClick()">添加菜品</a></li>
					@endif
				</ul>
			</div>
			<div class="tab-content" style="min-height: 300px;">
				<div class="tab-pane fade" id="tab-bottom-left1">
					<div class="table-responsive">
						@if($shop->extractlist->count()>0)
						<table class="table table-hover">
							<thead>
								<tr>
									<th>银行卡号</th>
									<th>持卡人</th>
									<th>提现金额</th>
									<th>申请时间</th>
									<th>处理进度</th>
								</tr>
							</thead>
							<tbody>
								@foreach($shop->extractlist as $k=>$v)
								<tr>
									<td>{{$v->bank_number}}</td>
									<td>{{$v->bank_name}}</td>
									<td>{{$v->amount}}</td>
									<td>{{$v->insert_time}}</td>
									<td>{{$extract_status_de[$v->extract_status]}}</td>
								</tr>
								@endforeach
							</tbody>
						</table>
						{{ $shop->extractlist->appends(['shopid' => $shop->shop_id])->fragment('tab-bottom-left1')->links() }}
						@else
						<h3>暂无提现记录</h3>
						@endif
				    </div>
				</div>
				<input type="hidden" name="shop_id" value="{{$shop->shop_id}}">
				<div class="tab-pane fade" id="tab-bottom-left2">
					<div class="table-responsive">
						@if($orders->count()>0)
						<table class="table table-hover">
							<thead>
								<tr>
									<th>订单号</th>
									<th>流水号</th>
									<th>下单用户</th>
									<th>下单时间</th>
									<th>用户电话</th>
									<th>下单餐品</th>
									<th>消费金额</th>
									@if(session('region') < 3)
									<th>订单状态</th>
									@endif
								</tr>
							</thead>
							<tbody>
								@foreach($orders as $k=>$v)
								<tr>
									<td>{{$v->order_number}}</td>
									<td>{{$v->order_sernum}}</td>
									<td>
										<a href="/user-details?userid={{$v->users_id}}">{{$v->nicename ?? '未填写'}}</a>
									</td>
									<td>{{$v->insert_time}}</td>
									<td>{{$v->user_tel}}</td>
									<td>{{$v->cateslist}}</td>
									<td>{{$v->meal_order_price}}</td>
									@if($v->order_status==3)
									<td>{{$order_status_de[$v->order_status]}}</td>
									@else
										@if(session('region') < 3)
										<td id="orderstatus">{{$order_status_de[$v->order_status]}} · <a href="javascript:void(0)" onclick="cancelOrder('{{$v->order_id}}')">撤销订单</a></td>
										@endif
									@endif
								</tr>
								@endforeach
							</tbody>
						</table>
						{{ $orders->appends(['shopid' =>  $shop->shop_id])->fragment('tab-bottom-left2')->links() }}
						@else
						<h3>暂无记录</h3>
						@endif
					</div>
				</div>
				<div class="tab-pane fade" id="tab-bottom-left3">
					<div class="table-responsive">
						@if($shop->shopcate->count()>0)
						<table class="table table-hover">
							<thead>
								<tr>
									<th>名称</th>
									<th>价格</th>
									<th>销量</th>
									<th>描述</th>
									<th>分类</th>
									<th>商品状态</th>
									<th>上架时间</th>
									@if(session('region') < 3)
									<th>操作</th>
									@endif
								</tr>
							</thead>
							<tbody>
								@foreach($shop->shopcate as $v)
								<tr>
									<td class="cate-name">{{$v->cate_name}}</td>
									<td class="cate_price">{{$v->cate_price}}</td>
									<td>{{$v->cate_sales}}</td>
									<td class="cate_desc">{{$v->cate_desc}}</td>
									<td class="sort_id" data-id="{{$v->sort_id}}">{{$v->sort_name}}</td>
									<td class="cate_status" data-id="{{$v->cate_status}}">{{$cate_status_de[$v->cate_status]}}</td>
									<td>{{date('Y-m-d H:i',$v->insert_time)}}</td>
									@if(session('region') < 3)
									<td><a href="javascript:void(0)" onclick="urlClick('{{$v->cate_imgs}}','{{$v->cate_id}}',this)">修改</a></td>
									@endif
								</tr>
								@endforeach
							</tbody>
						</table>
						{{ $shop->shopcate->appends(['shopid' => $shop->shop_id])->fragment('tab-bottom-left3')->links() }}
						@else
							<h3>他竟然什么都没有上传！</h3>
						@endif
					</div>
				</div>
			</div>
			<!-- END TABBED CONTENT -->
		</div>
		<!-- END RIGHT COLUMN -->
	</div>
</div>
<div class="modal fade" tabindex="-1" id="exampleModal" role="dialog">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">添加菜品</h4>
      </div>

      <form id="editcateimg" action="/save/cate/info" method="post" >
	      <div class="modal-body">
	      	  <div class="form-group">
		      	<label for="message-text" class="control-label">名称</label>
		      	<input type="input" name="cate_name" class="form-control form-input-control">
		      </div>
		      <div class="form-group">
		      	<label for="message-text" class="control-label">价格</label>
		      	<input type="input" name="cate_price" class="form-control form-input-control">
		      </div>
		      <div class="form-group">
		      	<label for="message-text" class="control-label">描述</label>
		      	<input type="input" name="cate_desc" class="form-control form-input-control">
		      </div>
		      <div class="form-group">
		      	<label for="message-text" class="control-label">分类</label>
		      	<select class="form-control form-input-control" name="sort_id">
		      		@foreach($sorts as $k=>$v)
		      		<option value="{{$v->sort_id}}">{{$v->sort_name}}</option>
		      		@endforeach
		      	</select>
		      </div>
		      <div class="form-group">
		      	<label class="fancy-radio">
					<input name="cate_status" value="1" type="radio">
					<span><i></i>上架</span>
				</label>
				<label class="fancy-radio">
					<input name="cate_status" value="0" type="radio">
					<span><i></i>下架</span>
				</label>
		      </div>
	      	  <div class="form-group">
		        <label for="message-text" class="control-label">新的图片（不可超过1M）</label>
		        <input type="file" name="cate_img">
		      </div>
		      <input type="hidden" name="cate_id" value="">
		      <input type="hidden" name="shop_id" value="{{$shop->shop_id}}">
	      </div>
	      <div class="modal-footer">
	        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
	        <button type="submit" class="btn btn-primary saveCateImage">保存</button>
	      </div>
     </form>
    </div>
  </div>
</div>
<div class="modal fade" tabindex="-1" id="editShopInfo" role="dialog">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title"></h4>
      </div>
      
      <form id="editShopInfoForm" action="/editShopInfo" method="post" >
	      <div class="modal-body">
	      	  <div class="form-group">
		        <label for="message-text" class="control-label">修改头像</label>
		        <input type="file" name="avatar">
		      </div>
		      @if(session('region') == 1)
		      <div class="form-group">
		      	<label for="message-text" class="control-label">重置密码</label>
		        <div class="switch form-input-control" data-on-label="重置" data-off-label="不重置">
				    <input type="checkbox" name="resetpass" value="1" />123456
				</div>
		      </div>
		      <div class="form-group">
		      	<label for="message-text" class="control-label">折扣设置(0.0 - 9.9)</label>
		        <input type="text" name="discount" placeholder="0.0 - 9.9" value="{{$shop->discount}}" class="form-control form-input-control">
		      </div>
		      @endif
		      <div class="form-group">
		      	<label for="message-text" class="control-label">不可打包</label>
		        <div class="switch form-input-control" data-on-label="不可打包" data-off-label="可打包">
				    <input type="checkbox" name="packfood" value="1" {{$shop->packfood_if==1?'checked="checked"':''}}/> 不可打包
				</div>
		      </div>
		      <div class="form-group">
		      	<label for="message-text" class="control-label">商铺名称</label>
		        <input type="text" name="shop_name" placeholder="商铺名称" value="{{$shop->shop_name}}" class="form-control form-input-control">
		      </div>
		      <div class="form-group">
		      	<label for="message-text" class="control-label">商铺地址</label>
		        <input type="text" name="shop_address" placeholder="商铺地址" value="{{$shop->shop_address}}" class="form-control form-input-control">
		      </div>
		      <input type="hidden" name="shop_id" value="{{$shop->shop_id}}">
	      </div>
	      <div class="modal-footer">
	        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
	        <button type="submit" class="btn btn-primary saveCateImage">保存</button>
	      </div>
     </form>
    </div>
  </div>
</div>
<img src="" class="identitycardimg identitycardimg_sty center-block hide">
@endsection

@section('js-write')
<script type="text/javascript">
	$('.nav').find('.tab-foodlist').trigger('click');
	function urlClick(url,cateid,this_l) {
		var this_ = $('<img src="" width="30" title="点击放大" class="qrcodeimg">');
		var modal = $('#exampleModal');
		modal.find('.modal-body .identitycardimg').remove();
		modal.find('.modal-body input[name="cate_id"]').val('');
		if(cateid){
			modal.find('.modal-body input[name="cate_id"]').val(cateid);
		}
		modal.find('.modal-body input[name="cate_name"]').val($(this_l).parents('tr').find('.cate-name').html());
		modal.find('.modal-body input[name="cate_desc"]').val($(this_l).parents('tr').find('.cate_desc').html());
		modal.find('.modal-body input[name="cate_price"]').val($(this_l).parents('tr').find('.cate_price').html());
		modal.find('.modal-body select[name="sort_id"]').val($(this_l).parents('tr').find('.sort_id').attr('data-id'));
		modal.find('.modal-body input[name="cate_status"]').each(function(){
			console.log($(this_l).parents('tr').find('.cate_status').attr('data-id'))
			if($(this).val() == $(this_l).parents('tr').find('.cate_status').attr('data-id')){
				$(this).attr('checked',true);
			}
		});
		modal.find('.modal-body input[name="cateimg"]').val('');
		if(this_l){
			modal.find('.modal-title').html($(this_l).parents('tr').find('.cate-name').html());
		}else{
			modal.find('.modal-title').html('添加菜品');
		}
		if(url){
			$(this_).attr('src',url).attr('class','identitycardimg identitycardimg_sty center-block');
	    	modal.find('.modal-body').append($(this_));
		}
		modal.modal();
	}

	function editInfo()
	{
		var modal = $('#editShopInfo');
		modal.modal();
	}

	var SendParam = {
		'dataType':'json',
		'beforeSubmit':cateUpBefore,
		'success':cateUpResponse,
		'error':cateUpError,
	};
	var SendParamInfo = {
		'dataType':'json',
		'beforeSubmit':cateUpBefore,
		'success':cateUpResponse,
		'error':cateUpError,
	};

	function cateUpBefore(formData, jqForm, options){
		
	}

	function cateUpResponse(response) {
		if(response.success){
			setTimeout(function(){
	            location.href = location.href;
	            // urlClick();
	        },2000);
			toastr.success(response.message);
		}else{
			toastr.error(response.message);
		}
	}

	function cateUpError(XMLHttpRequest, textStatus, errorThrown) {

		if(XMLHttpRequest.status==422){

			var RegError = XMLHttpRequest.responseJSON.errors;

			for(var o in RegError){

				for (var i = 0; i < RegError[o].length; i++) {

					toastr.error(RegError[o][i]);

				}

			}

		}

	}

	ajaxForm_('#editcateimg', SendParam);

	ajaxForm_('#editShopInfoForm', SendParamInfo);

	$('input[type=file][name=cateimg]').change(function(e){

		var files = e.target.files;

		var v = $(this).val();

		var reader = new window.FileReader();

		reader.readAsDataURL(files[0]);

		//异步加载文件成功
	    reader.onload = function(e){
	       // reader.result 表示图片地址
	       // $('#exampleModal .modal-footer .saveCateImage').removeClass('disabled').removeAttr('disabled');
	       var this_ = $('<img src="" width="30" title="点击放大" class="qrcodeimg">');
	       $(this_).attr('src','').attr('class','identitycardimg identitycardimg_sty center-block');
	       $('#exampleModal').find('.modal-body').append($(this_));
	       $('#exampleModal .modal-body .identitycardimg').attr('src',reader.result);
	    }

	});

	var m = location.hash;
	$('a[href="'+m+'"]').tab('show');
	$('.suspend-business').click(suspendBusiness);
	$('.start-business').click(startBusiness);
	function controlswitch(action,id) {
		$.ajaxSetup({
		    headers: {
		        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
		    }
		});
		$.post('/edit-shopstatus',{'action':action,'shopid':id},function(response){
			if(response.success){
				if(action=='ZT'){
					$('.suspend-business').unbind('click');
					$('.suspend-business').addClass('start-business');
					$('.start-business').removeClass('suspend-business').html('开始营业');
					$('.start-business').click(startBusiness);
					$('.run-status span').attr('class','fa fa-play award-icon').attr('title','暂停营业');
				}
				if(action=='KQ'){
					$('.start-business').unbind('click');
					$('.start-business').addClass('suspend-business');
					$('.suspend-business').removeClass('start-business').html('暂停营业');
					$('.suspend-business').click(suspendBusiness);
					$('.run-status span').attr('class','fa fa-circle-o-notch fa-spin fa-3x fa-fw margin-bottom').attr('title','正在营业');
				}
				toastr.success(response.message);
			}else{
				toastr.error(response.message);
			}
		},'json');	
	}
	function startBusiness(){
		swal({
	 	 title: '确定要将店铺开始营业吗？', 
		  text: '确定后该店铺及店铺餐品会展示在APP端', 
		  type: 'question',
		  showCancelButton: true, 
		  confirmButtonColor: '#3085d6',
		  cancelButtonColor: '#d33',
		  confirmButtonText: '确定', 
		  cancelButtonText: '取消',
		  confirmButtonClass: 'btn btn-success',
		  cancelButtonClass: 'btn btn-danger',
		  buttonsStyling: true
		}).then(function(al) {
			if(al.value){
				controlswitch('KQ',$('input[name="shop_id"]').val());
			}
		});
	}
	function suspendBusiness(){
		swal({
		  title: '确定暂停该店铺营业吗？', 
		  text: '暂停后该店铺及该店铺的餐品将不展示在APP端', 
		  type: 'question',
		  showCancelButton: true, 
		  confirmButtonColor: '#3085d6',
		  cancelButtonColor: '#d33',
		  confirmButtonText: '确定', 
		  cancelButtonText: '取消',
		  confirmButtonClass: 'btn btn-success',
		  cancelButtonClass: 'btn btn-danger',
		  buttonsStyling: true
		}).then(function(al) {
			if(al.value){
				controlswitch('ZT',$('input[name="shop_id"]').val());
			}
		});
	}

	function cancelOrder(orderid) {

		swal({
	 	 title: '确定撤回该订单吗？', 
		  text: '操作将返还用户的消费次数，并扣除店铺收入。', 
		  type: 'question',
		  showCancelButton: true, 
		  confirmButtonColor: '#3085d6',
		  cancelButtonColor: '#d33',
		  confirmButtonText: '确定', 
		  cancelButtonText: '取消',
		  confirmButtonClass: 'btn btn-success',
		  cancelButtonClass: 'btn btn-danger',
		  buttonsStyling: true
		}).then(function(al) {
			if(al.value){
				$.ajaxSetup({
				    headers: {
				        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
				    }
				});
				$.post('/cancel-order',{'order_id':orderid},function(response){
					if(response.success){
						$('#orderstatus').text("已取消");
						toastr.success(response.message);
					}else{
						toastr.error(response.message);
					}
				},'json');	
			}
		});
	}
</script>
@endsection